<template>
  <!--
  调用接口：
    1：封装api
    2: 导入
    3：调用
 -->

  <div class="login">
    <div class="box">
      <img class="logo" src="@/assets/common/login-logo.png" alt="">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
        </el-form-item>
        <el-form-item prop="password">
          <!-- .native:让组件标签可使用原生的事件方法 -->
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter.native="submit"
          />
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submit">{{
            $t('login.login')
          }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { sysLogin } from '@/api/user'
export default {
  data() {
    return {
      form: {
        mobile: '13800000002', //	string	必须		手机号
        password: '123456' //	string	必须		密码
      },
      rules: {
        mobile: [
          { required: true, message: this.$t('login.any'), trigger: 'change' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请正确输入手机号',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: this.$t('login.any'), trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async submit() {
      // this.$refs.form.validate((result) => {
      //   console.log('验证结果', result)
      // })
      // try {
      //   await this.$refs.form.validate()
      //   console.log('验证成功')
      // } catch {
      //   console.log('验证失败')
      // }
      await this.$refs.form.validate()
      // try {
      const res = await sysLogin(this.form)

      // 提示
      this.$message.success('登录成功')
      // 存储token（存储在vuex,token要持久化）
      this.$store.commit('user/SET_TOKEN', res.data)

      // 跳转首页
      // this.$router.push('/')
      this.$router.replace('/') // 用跳转页面替换当前页面，回退就回不到当前页面了
      console.log('登录返回值', res)
      // } catch (err) {
      //   console.log('错误', err)
      // }
    }
  }
}
</script>
<style lang="scss" scoped>
// @代表src,  style内有版本问题，vue-cli5:@与~@都代表src,但之前 版本只有~@代表src
.login {
  height: 100vh;
  background-image: url('~@/assets/common/login.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    .logo {
      margin-bottom: 30px;
    }
    ::v-deep .el-input__inner {
      height: 48px;
      background-color: #d3e4ff;
    }
    ::v-deep .el-form-item__error {
      color: #fff;
    }
    .btn {
      width: 100%;
      height: 60px;
      font-size: 24px;
    }
  }
}
</style>
